package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.ad.api.AduserattrSaveCmd;
import com.jackrain.nea.ad.mapper.AduserattrMapper;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * @author nathan
 */

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AduserattrSaveCmdImpl extends CommandAdapter implements AduserattrSaveCmd {
    @Override
    public ValueHolder execute(QuerySession querySession) throws NDSException {
        ValueHolder valueHolder = new ValueHolder();
        AduserattrMapper aduserattrMapper = ApplicationContextHandle.getBean(AduserattrMapper.class);
        DefaultWebEvent event = querySession.getEvent();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(event.getParameterValue("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        JSONObject fixColumn = param.getJSONObject("fixcolumn");
        JSONObject jo = fixColumn.getJSONObject("AD_USER_ATTR");
        int clientId = querySession.getUser().getClientId();
        Integer userId = querySession.getUser().getId();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        if (jo != null) {
            if (StringUtils.isNotEmpty(param.getString("objid")) && param.getLong("objid") > 0) {
                Long objId = param.getLong("objid");
                jo.put("ID", objId);
                //查询此记录是否存在
                int idCount = aduserattrMapper.queryuserattrById(objId);
                if (idCount == 0){
                    throw new NDSException(Resources.getMessage("当前记录已不存在！",querySession.getLocale()));
                }

                //查询名称是否相同
                String eName = jo.getString("NAME");
                if (StringUtils.isNotEmpty(eName)){
                    int eNameCount = aduserattrMapper.queryuserattrByENameForUp(objId,eName);
                    if (eNameCount>0){
                        throw new NDSException(Resources.getMessage("输入的数据已存在：名称",querySession.getLocale()));
                    }
                }

                jo.put("MODIFIEDDATE",timestamp);
                jo.put("MODIFIERID",Integer.valueOf(userId).longValue());
                jo.put("MODIFIERNAME",querySession.getUser().getName());
                jo.put("MODIFIERENAME",querySession.getUser().getEname());
                int result = aduserattrMapper.userattrUppdate(jo);
                if (result==0){
                    valueHolder.put("code",-1);
                    valueHolder.put("message",Resources.getMessage("当前记录已不存在！",querySession.getLocale()));
                    return valueHolder;
                }
            }else {
                //新增操作
                //判断名称是否唯一
                String eName = jo.getString("NAME");
                int eNameCount = aduserattrMapper.queryuserattrByEName(eName);
                if (eNameCount==1){
                    throw new NDSException(Resources.getMessage("输入的数据已存在：名称",querySession.getLocale()));
                }
                jo.put("AD_CLIENT_ID",Integer.valueOf(clientId).longValue());
                jo.put("AD_ORG_ID",Integer.valueOf(querySession.getUser().getOrgId()).longValue());
                jo.put("ISACTIVE","Y");
                jo.put("OWNERID",Integer.valueOf(userId).longValue());
                jo.put("OWNERNAME",querySession.getUser().getName());
                jo.put("OWNERENAME",querySession.getUser().getEname());
                jo.put("CREATIONDATE",timestamp);
                jo.put("MODIFIEDDATE",timestamp);
                jo.put("MODIFIERID",Integer.valueOf(userId).longValue());
                jo.put("MODIFIERNAME",querySession.getUser().getName());
                jo.put("MODIFIERENAME",querySession.getUser().getEname());
                Long objid = Tools.getSequence("AD_USER_ATTR");
                jo.put("ID", objid);
                aduserattrMapper.userattrAdd(jo);
                JSONObject dataJson = new JSONObject(true);
                dataJson.put("tablename","AD_USER_ATTR");
                dataJson.put("objid",objid);
                valueHolder.put("data",dataJson);
            }
        }
        valueHolder.put("code",0);
        valueHolder.put("message",Resources.getMessage("成功",querySession.getLocale()));
        return valueHolder;
    }
}
